﻿Imports System.Windows.Forms

Public Class StartupOrder
    Dim dirs As ArrayList
    Dim exts As ArrayList
    Dim names As ArrayList

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        My.Settings.prognames = names
        My.Settings.progdirs = dirs
        My.Settings.progexts = exts
        My.Settings.Save()
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.Close()
    End Sub

    Private Sub StartupOrder_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        names = New ArrayList
        dirs = New ArrayList
        exts = New ArrayList
        ListBox1.Items.Clear()
        For Each item In My.Settings.prognames
            ListBox1.Items.Add(item)
            names.Add(item)
            dirs.Add(My.Settings.progdirs(My.Settings.prognames.IndexOf(item)))
            exts.Add(My.Settings.progexts(My.Settings.prognames.IndexOf(item)))
        Next
    End Sub

    Private Sub Up_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Up.Click
        If Not ListBox1.SelectedIndex = -1 And Not ListBox1.SelectedIndex = 0 Then
            Dim item As String = ListBox1.SelectedItem
            Dim index As Integer = ListBox1.SelectedIndex
            Dim dir As String = dirs(index)
            Dim ext As String = exts(index)
            names.RemoveAt(index)
            dirs.RemoveAt(index)
            exts.RemoveAt(index)
            ListBox1.Items.RemoveAt(index)
            index -= 1
            names.Insert(index, item)
            dirs.Insert(index, dir)
            exts.Insert(index, ext)
            ListBox1.Items.Insert(index, item)
            ListBox1.SelectedIndex = index
        End If
    End Sub

    Private Sub Down_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Down.Click
        If Not ListBox1.SelectedIndex = -1 And Not ListBox1.SelectedIndex = 0 Then
            Dim item As String = ListBox1.SelectedItem
            Dim index As Integer = ListBox1.SelectedIndex
            Dim dir As String = dirs(index)
            Dim ext As String = exts(index)
            names.RemoveAt(index)
            dirs.RemoveAt(index)
            exts.RemoveAt(index)
            ListBox1.Items.RemoveAt(index)
            index += 1
            names.Insert(index, item)
            exts.Insert(index, ext)
            dirs.Insert(index, dir)
            ListBox1.Items.Insert(index, item)
            ListBox1.SelectedIndex = index
        End If
    End Sub

    Private Sub SaveOrder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveOrder.Click
        If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim sw As New System.IO.StreamWriter(SaveFileDialog1.FileName)
            For Each item In names
                sw.WriteLine(item & "|" & dirs(names.IndexOf(item)) & "|" & exts(names.IndexOf(item)))
            Next
            sw.Close()
        End If
    End Sub

    Private Sub LoadOrder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadOrder.Click
        If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim sr As New System.IO.StreamReader(OpenFileDialog1.FileName)
            names.Clear()
            dirs.Clear()
            exts.Clear()
            ListBox1.Items.Clear()
            Do
                Try
                    Dim name As String = ""
                    Dim dir As String = ""
                    Dim ext As String = ""
                    Dim line As String = sr.ReadLine()
                    Dim i As Integer = 0
                    Try
                        Do Until line.Chars(i) = "|"
                            name += line.Chars(i)
                            i += 1
                        Loop
                        i += 1
                        Do Until line.Chars(i) = "|"
                            dir += line.Chars(i)
                            i += 1
                        Loop
                        i += 1
                        Do Until i = line.Length
                            ext += line.Chars(i)
                            i += 1
                        Loop
                    Catch
                        Exit Sub
                        MsgBox("This file is not valid. It may be incompatible with this version of startup manager. Sorry for any inconvenience")
                    End Try
                    ListBox1.Items.Add(name)
                    names.Add(name)
                    dirs.Add(dir)
                    exts.Add(ext)
                Catch
                    Exit Do
                End Try
            Loop
            sr.Close()
        End If
    End Sub
End Class
